package com.wangxiaotao.springcloud.alibaba.service.impl;

import com.wangxiaotao.springcloud.alibaba.dao.OrderDao;
import com.wangxiaotao.springcloud.alibaba.entity.Order;
import com.wangxiaotao.springcloud.alibaba.service.AccountService;
import com.wangxiaotao.springcloud.alibaba.service.OrderService;
import com.wangxiaotao.springcloud.alibaba.service.StorageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@RequiredArgsConstructor
public class OrderServiceImpl implements OrderService {
    private final OrderDao orderDao;
    private final AccountService accountService;
    private final StorageService storageService;
    @Override
    public void create(Order order) {
        log.info("----->新建订单 start");
        orderDao.create(order);

        log.info("----->订单微服务开始调用库存扣减 start");
        storageService.decrease(order.getProductId(),order.getCount());
        log.info("----->订单微服务开始调用库存扣减 end");

        log.info("----->订单微服务开始调用账户余额扣减 start");
        accountService.decrease(order.getUserId(),order.getMoney());
        log.info("----->订单微服务开始调用账户余额扣减 end");

        log.info("----->修改订单状态 start");
        orderDao.update(order.getUserId(),0);
        log.info("----->修改订单状态 end");

        log.info("----->新建订单 end");
    }

}
